package redis.in.action.exercise;

import org.apache.commons.lang.StringUtils;

import redis.clients.jedis.Jedis;

public class Chapter02 {

	private static final String VIEWED = "viewed:";
	private static final String RECENT = "recent:";
	private static final String LOGIN = "login:";

	public static void main(String[] args) {
	}

	public String checkToken(Jedis jedis, String token) {
		return jedis.hget(LOGIN, token);
	}

	public void updateToken(Jedis jedis, String token, String user, String item) {
		long now = System.currentTimeMillis();
		jedis.hset(LOGIN, token, user);
		jedis.zadd(RECENT, now, token);
		if (StringUtils.isNotEmpty(item)) {
			String key = VIEWED + token;
			jedis.zadd(key, now, item);
			jedis.zremrangeByRank(key, 0, -26);
		}
	}
	

}
